<?
	class Picture {
		private $fileName;
		public $width;
		public $height;
		private $thumbDir;
		private $extension;
		public $loaded = false;
		
		public function __construct($fileName,$thumbDir = '/tmp/') {
			$this->fileName = $fileName;
			$this->thumbDir = $thumbDir;
			list($this->width, $this->height) = @getimagesize($this->fileName);
            $pathParts = pathinfo($fileName);
			$this->extension = $pathParts['extension'];
			
			if (($this->width*$this->height) != 0){
				$this->loaded = true;	
			}
		}
		private function loadImageFromFile() {
			$origImage = null;
			if (strcasecmp($this->extension,'png') == 0){
				
				$origImage = imagecreatefrompng($this->fileName);
				
			}
			if (strcasecmp($this->extension,'jpg') == 0 || strcasecmp($this->extension,'jpeg') == 0)
				$origImage = imagecreatefromjpeg($this->fileName);
			if (strcasecmp($this->extension,'gif') == 0)
				$origImage = imagecreatefromgif($this->fileName);
			return $origImage;
		}
		private function saveImageToFile($thumbImage, $thumbFileName) {
			if (strcasecmp($this->extension,'png') == 0){
				
				imagepng($thumbImage,$thumbFileName);
			}
			if (strcasecmp($this->extension,'jpg') == 0 || strcasecmp($this->extension,'jpeg') == 0)
				imagejpeg($thumbImage,$thumbFileName,90);
			if (strcasecmp($this->extension,'gif') == 0)
				imagegif($thumbImage,$thumbFileName);

		}
		// ext - may be save thump in other format,eraseOldThumb - if true, will erase old thumb if any
		public function createThumb($thumbMaxWidth = 80, $thumbMaxHeight = 60,$ext = "", $eraseOldThumb = false) {
			//get thumb file name
			$origImage = $this->loadImageFromFile();
			
			if (strlen($ext)>0){
				$thumbBaseName = basename($this->fileName) . "-thumb.$ext";
			} else {
				$thumbBaseName = basename($this->fileName) . "-thumb.$this->extension";
			}
			$thumbFileName = $this->thumbDir . $thumbBaseName;
			if ($eraseOldThumb){
				@unlink($thumbFileName);
			}
			if (!file_exists($thumbFileName)) {
				$origRatio = $this->width / $this->height;
				$thumbRatio = $thumbMaxWidth / $thumbMaxHeight;
				$newWidth = $newHeight = 0;
				if ($origRatio > $thumbRatio) {
					$newWidth = $thumbMaxWidth;
					$newHeight = $this->height * ($thumbMaxWidth / $this->width);
				} else {
					$newHeight = $thumbMaxHeight;
					$newWidth = $this->width * ($thumbMaxHeight / $this->height);
				}
				$this->smart_resize_image( $this->fileName, $newWidth, $newHeight, true, $thumbFileName, false, false );
    	
				/*
				$thumbImage = imagecreatetruecolor($newWidth, $newHeight);
				//$thumbImage = imagecreatetruecolor($this->width, $this->height);
				imagesavealpha($thumbImage,true);
				imageAlphaBlending($thumbImage, true);
				imagesavealpha($origImage,true);
				imageAlphaBlending($origImage, true);
				$transparent = imagecolorallocatealpha($thumbImage, 0, 0, 0, 127);
				imagefill($thumbImage, 0, 0, $transparent);

				imagecopyresized($thumbImage, $origImage, 0, 0, 0, 0, $newWidth, $newHeight, $this->width, $this->height);
				//imagecopy($thumbImage, $origImage, 0, 0, 0, 0, $this->width, $this->height,100);
				//$a = array($thumbImage,$origImage);
				
				//$this->saveImageToFile($thumbImage, $thumbFileName);
				//$this->saveImageToFile($this->imagemergealpha($a), $thumbFileName);
				*/
				
				//chmod($thumbFileName, 0644);
			}
			return $thumbBaseName;
		}		
		
		public function copyResized($newWidth, $newHeight) {
			$baseName = basename($this->fileName);
			$name = substr($baseName, 0, strrpos($baseName,'.'));
			$ext = substr($baseName, strrpos($baseName,'.'));
			$newName = dirname($this->fileName) . '/' . $name . '-' .
					$newWidth . 'x' . $newHeight . $ext;
			$origImage = $this->loadImageFromFile();
            $newImage = imagecreatetruecolor($newWidth, $newHeight);
			imagecopyresampled($newImage, $origImage, 0, 0, 0, 0, $newWidth,
					$newHeight, $this->width, $this->height);
			$this->saveImageToFile($newImage, $newName);
			chmod($newName, 0644);
			return $newName;
        }
        public function saveResized($newWidth, $newHeight) {
            $origImage = $this->loadImageFromFile();
            $newImage = imagecreatetruecolor($newWidth, $newHeight);
			imagecopyresampled($newImage, $origImage, 0, 0, 0, 0, $newWidth,
					$newHeight, $this->width, $this->height);
			$this->saveImageToFile($newImage, $this->fileName);
			chmod($this->fileName, 0644);
        }
        public function getWidth() {
        	return $this->width;
        }
        public function getHeight() {
        	return $this->height;
        }
        
        function smart_resize_image( $file, $width = 0, $height = 0, $proportional = false, $output = 'file', $delete_original = true, $use_linux_commands = false )
    	{
			        if ( $height <= 0 && $width <= 0 ) {
			            return false;
			        }

			       
			$info = getimagesize($file);
			        $image = '';

			       
			$final_width = 0;
			        $final_height = 0;
			        list($width_old, $height_old) = $info;

			        if (
			$proportional) {
			            if ($width == 0) $factor = $height/$height_old;
			            elseif ($height == 0) $factor = $width/$width_old;
			            else $factor = min ( $width / $width_old, $height / $height_old);  

			           
			$final_width = round ($width_old * $factor);
			            $final_height = round ($height_old * $factor);

			        }
			        else {
			           
			$final_width = ( $width <= 0 ) ? $width_old : $width;
			            $final_height = ( $height <= 0 ) ? $height_old : $height;
			        }

			        switch (
			$info[2] ) {
			            case IMAGETYPE_GIF:
			                $image = imagecreatefromgif($file);
			            break;
			            case IMAGETYPE_JPEG:
			                $image = imagecreatefromjpeg($file);
			            break;
			            case IMAGETYPE_PNG:
			                $image = imagecreatefrompng($file);
			            break;
			            default:
			                return false;
			        }
			       
			        $image_resized = imagecreatetruecolor( $final_width, $final_height );
			               
			        if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
			            $trnprt_indx = imagecolortransparent($image);
			  
			            // If we have a specific transparent color
			            if ($trnprt_indx >= 0) {
			  
			                // Get the original image's transparent color's RGB values
			                $trnprt_color    = imagecolorsforindex($image, $trnprt_indx);
			  
			                // Allocate the same color in the new image resource
			                $trnprt_indx    = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
			  
			                // Completely fill the background of the new image with allocated color.
			                imagefill($image_resized, 0, 0, $trnprt_indx);
			  
			                // Set the background color for new image to transparent
			                imagecolortransparent($image_resized, $trnprt_indx);
			  
			           
			            }
			            // Always make a transparent background color for PNGs that don't have one allocated already
			            elseif ($info[2] == IMAGETYPE_PNG) {
			  
			                // Turn off transparency blending (temporarily)
			                imagealphablending($image_resized, false);
			  
			                // Create a new transparent color for image
			                $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
			  
			                // Completely fill the background of the new image with allocated color.
			                imagefill($image_resized, 0, 0, $color);
			  
			                // Restore transparency blending
			                imagesavealpha($image_resized, true);
			            }
			        }

			       
			imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
			   
			        if ( $delete_original ) {
			            if ( $use_linux_commands )
			                exec('rm '.$file);
			            else
			                @unlink($file);
			        }
			       
			        switch ( strtolower($output) ) {
			            case 'browser':
			                $mime = image_type_to_mime_type($info[2]);
			                header("Content-type: $mime");
			                $output = NULL;
			            break;
			            case 'file':
			                $output = $file;
			            break;
			            case 'return':
			                return $image_resized;
			            break;
			            default:
			            break;
			        }

			        switch (
			$info[2] ) {
			            case IMAGETYPE_GIF:
			                imagegif($image_resized, $output);
			            break;
			            case IMAGETYPE_JPEG:
			                imagejpeg($image_resized, $output);
			            break;
			            case IMAGETYPE_PNG:
			                imagepng($image_resized, $output);
			            break;
			            default:
			                return false;
			        }

			        return
			true;
		}
        function imagemergealpha($i) {

			//create a new image
			$s = imagecreatetruecolor(imagesx($i[0]),imagesy($i[1]));

			//merge all images
			imagealphablending($s,true);
			$z = $i;
			while($d = each($z)) {
			imagecopy($s,$d[1],0,0,0,0,imagesx($d[1]),imagesy($d[1]));
			}

			//restore the transparency
			imagealphablending($s,false);
			$w = imagesx($s);
			$h = imagesy($s);
			for($x=0;$x<$w;$x++) {
			for($y=0;$y<$h;$y++) {
			$c = imagecolorat($s,$x,$y);
			$c = imagecolorsforindex($s,$c);
			$z = $i;
			$t = 0;
			while($d = each($z)) {
			$ta = imagecolorat($d[1],$x,$y);
			$ta = imagecolorsforindex($d[1],$ta);
			$t += 127-$ta['alpha'];
			}
			$t = ($t > 127) ? 127 : $t;
			$t = 127-$t;
			$c = imagecolorallocatealpha($s,$c['red'],$c['green'],$c['blue'],$t);
			imagesetpixel($s,$x,$y,$c);
			}
			}
			imagesavealpha($s,true);
			return $s;
		}
	}
	
	
	
	
	
?>
